﻿using Acme.SmartWMS.RBAC.UserRoles.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Users;

namespace Acme.SmartWMS.RBAC.UserRoles
{
    public class UserRolesAppSerivce : ApplicationService, IUserRolesAppSerivce
    {
        /// <summary>
        /// 用户角色
        /// </summary>
        private readonly IRepository<UserRole> userRoleRep;
        /// <summary>
        /// 用户
        /// </summary>
        private readonly IRepository<Role> roleRep;

        public UserRolesAppSerivce(IRepository<UserRole> userRoleRep, IRepository<Role> roleRep)
        {
            this.userRoleRep = userRoleRep;
            this.roleRep = roleRep;
        }

        /// <summary>
        /// 查询用户所对应的角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<List<UserRoleDto>> GetUserInRoles(int userId)
        {
            // 异步获取用户角色和角色列表
            var userRoles = from userRole in await userRoleRep.GetListAsync() // 获取用户角色
                            join role in await roleRep.GetListAsync() // 获取角色列表
                            on userRole.RoleId equals role.Id // 联接条件
                            where userRole.UserId == userId // 过滤条件
                            select new UserRoleDto
                            {
                                RoleId = userRole.RoleId,
                                RoleName = role.RoleName // 假设角色对象有 Name 属性
                            };

            return userRoles.ToList(); // 返回结果
        }

        /// <summary>
        /// 分配角色给用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleNames"></param>
        /// <returns></returns>
        public async Task<int> AllotRoles(AddUserRoleDto addDto)
        {
            var list = await userRoleRep.GetListAsync(x => x.UserId == addDto.UserId);
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    #region 删除
                    //var user = userRepository.GetAll().FirstOrDefault(x => x.UserId == request.UserId);

                    // 获取所有符合 UserId 的 UserRole 对象



                    // 删除这些 UserRole 对象
                    foreach (var userRole in list)
                    {
                        userRoleRep.DeleteAsync(userRole);
                    }
                    // 删除这些 UserRole 对象

                    //user.RoleId = "";
                    //userRepository.Update(user);

                    #endregion

                    #region 添加



                    for (int i = 0; i < addDto.RoleName.Split(',').Length; i++)
                    {

                        UserRole userRole = new UserRole();
                        userRole.UserId = addDto.UserId;
                        string a = addDto.RoleName.Split(',')[i];
                        userRole.RoleId = Convert.ToInt32(a);
                        userRoleRep.InsertAsync(userRole);

                        //var sss = userRoleRep.FindAsync(x => x.RoleId == Convert.ToInt32(a));
                        //if (i + 1 == addDto.RoleName.Split(',').Length)
                        //{
                        //    list.RoleId += sss.RoleName;
                        //}
                        //else
                        //{
                        //    user.RoleId += sss.RoleName + ',';
                        //}



                        //userRepository.Update(user);
                    }



                    #endregion

                    //提交
                    scope.Complete();
                }
                catch (Exception exp)
                {
                    return -1;
                }

                return 1;

            }


        }
    }
}
